<?php

namespace App\Http\ViewComposers;

use App\Models\ArticleCategory;
use Illuminate\View\View;

class ArticleCategoryComposer
{
    public function compose(View $view)
    {
        $articleCategoryModel = new ArticleCategory();
        $nav = ArticleCategory::where('parent_id', 0)
            ->orderBy('order', 'desc')
            ->get();
        //将文章和分类合并 并排序
        if ($nav->isNotEmpty()) {
            foreach ($nav as $article_category) {
                $article_category->son = $articleCategoryModel->handle_category_data($article_category->articles, $article_category->children);
            }
        }
        $nav_foot = ArticleCategory::where('parent_id', 0)
            ->orderBy('order')
            ->get();
        //将文章和分类合并 并排序
        if ($nav_foot->isNotEmpty()) {
            foreach ($nav_foot as $article_category) {
                $articles_footer = $article_category->articles()
                    ->where('is_footer', 1)
                    ->get();
                $article_category->son = $articleCategoryModel->handle_category_data($articles_footer, $article_category->children);
            }
        }


        $view->with(compact('nav', 'nav_foot'));
    }
}
